﻿Imports LuceneSearch.Monitoring.Interfaces
Imports DotNetNuke.Services.Log.EventLog
Imports Aricie.DNN.Modules.LuceneSearch.Settings

Namespace LuceneSearch.Monitoring.Monitors

    Public Class UpgradeMonitor
        Implements ILogInfoProvider

        Public Property ItemsUpgraded As Integer = 0
        Public Property ItemsSkipped As Integer = 0
        Public Property ItemsIgnored As Integer = 0
        Private Property UpgradingTime As TimeSpan
        Public Property UpgradeWatch As New Stopwatch()

        Public Sub New(LPS As LucenePortalSettings)
            UpgradingTime = TimeSpan.FromSeconds(LPS.NbSecondsUpgrading)
        End Sub

        Public ReadOnly Property UpgradeTimeExceeded As Boolean
            Get
                Return UpgradeWatch.Elapsed > UpgradingTime
            End Get
        End Property

        Public ReadOnly Property TotalItems As Integer
            Get
                Return ItemsUpgraded + ItemsSkipped + ItemsIgnored
            End Get
        End Property

        Public Function GetLogInfos() As System.Collections.Generic.IEnumerable(Of DotNetNuke.Services.Log.EventLog.LogInfo) Implements Interfaces.ILogInfoProvider.GetLogInfos
            Dim UpgradeLogInformations As New List(Of LogInfo)

            Dim UpgradeLogInfo As New LogInfo()
            UpgradeLogInfo.AddProperty("LuceneSearch upgrade", String.Format("Total items: {0}", TotalItems))
            UpgradeLogInfo.AddProperty("Upgraded items", ItemsUpgraded.ToString())
            UpgradeLogInfo.AddProperty(String.Format("Time available for upgrade ({0}s) exceeded", UpgradingTime.TotalSeconds), UpgradeTimeExceeded.ToString())
            UpgradeLogInfo.AddProperty("Time elapsed during upgrade", String.Format("{0}s", UpgradeWatch.Elapsed.TotalSeconds))
            UpgradeLogInfo.AddProperty("Skipped items", ItemsSkipped.ToString())
            UpgradeLogInfo.AddProperty("Ignored items (no upgrade available)", ItemsIgnored.ToString())

            UpgradeLogInformations.Add(UpgradeLogInfo)
            Return UpgradeLogInformations.Skip(0) ' returns a true enumerable
        End Function
    End Class

End Namespace